﻿#Region "Imports"
Imports System
Imports System.Linq
Imports System.Xml.Linq
Imports System.Collections.Generic
Imports GenDotNet.GenDotNet
Imports GenDotNet.TemplateSupport
Imports GenDotNet.TemplateSupport.Strings
#If CSharp Then
Imports GenDotNet.TemplateSupport.CSharp
#Else
Imports GenDotNet.TemplateSupport.VB
#End If
Imports GenDotNet.Task.Common
#End Region

Public Class GenerateEditableRoot
	Inherits DotNetLanguageBase

#Region "Required methods with static contents"
	Public Sub New()
		MyBase.New()
	End Sub

	' NEWTEMPLATE: Change to new guid for every new template
	Private ReadOnly _templateGuid As New Guid("{3021248B-5DB0-4c4d-AA75-65AEB2E4CA36}")
	Public Overrides ReadOnly Property Id() As System.Guid
		Get
			Return _templateGuid
		End Get
	End Property

	Protected Overrides Function GetFileImports() As TemplateSupport.ImportInfoCollection
		Return New CslaTemplate.Support.CslaImportInfoCollection
	End Function

   Protected Overrides Function GetFileName() As String
      Return OutputFileName(ObjectData.ClassName.GeneratedName)
   End Function

#End Region

	Protected Overrides Function GenerateContents() As String

		Dim classInfo = New TypeInfo(ObjectData.ClassName.BaseName, Scope.Protected, TypeCategory.Class, TypeModifiers.MustInherit)
		classInfo.InheritsFrom = New TypeInfo(CslaTemplate.Support.Type.BusinessBase, ObjectData.ClassName)
      classInfo.Attributes.AddSerializable()

		Return _
		<code>
				      ''------------------------------------------------------------------------------
    '' &lt;autogenerated>
    ''     This code was generated using CSLA 3.5 Template Collection.
    ''     Changes to this file may be lost if the code is regenerated.
    ''     Modify user class <%= ObjectData.ClassName %>.vb to extend this generated code. 
    ''
    ''     Code was generated at <%= Date.Now.ToString %> by <%= My.User.Name %>
    ''     Template path: <%= My.Application.Info.DirectoryPath %>
    ''     Template website: TBA
    '' &lt;/autogenerated>
    ''------------------------------------------------------------------------------

		</code>.Value & _
		OutputClass(classInfo, _
		 Function() ClassGenRootObject())
	End Function

	Protected Function ClassGenRootObject() As String
		Return _
		<code>
			<%= MemberConstructor() %>

			<%= BusinessMethods() %>
		</code>.Value
	End Function

	Protected Function MemberConstructor() As String
		If ObjectData.Properties.Count > 0 Then

			Return OutputConstructor(Scope.Protected, Function() _
			<code>
				' Requires use of Factory method
			</code>.Value)
		End If
		Return String.Empty
	End Function

	Protected Function BusinessMethods() As String
		Return _
		  <code>
			  <%= From x In ObjectData.Properties Select _
				  <code>
	Private Shared <%= x.Name %>Property As PropertyInfo(Of <%= x.NetType %>) = RegisterProperty(Of <%= x.NetType %>)(GetType(<%= ObjectData.ClassName %>), New PropertyInfo(Of <%= x.NetType %>)("<%= x.Caption %>"))
	Public Property <%= x.Name %>() As <%= x.NetType %>
    Get
            Return GetProperty(Of <%= x.NetType %>)(<%= x.Name %>Property)
    End Get
    Set(ByVal value As <%= x.NetType %>)
            SetProperty(Of <%= x.NetType %>)(<%= x.Name %>Property, value)
    End Set
  End Property
			</code>.Value %>
		  </code>.Value
	End Function
End Class
